<template>
  <div class="app-container">
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="temp"
      label-position="right"
      label-width="100px"
      style="width: 600px; margin-left:50px;"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="temp.title" />
      </el-form-item>
      
      <el-form-item label="类型" prop="type">
        <el-select v-model="temp.type" class="filter-item">
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="内容" prop="content">
        <el-input
          v-model="temp.content"
          type="textarea"
          :rows="10"
          placeholder="请输入内容"
        />
      </el-form-item>
      
      <el-form-item label="附件">
        <el-upload
          class="upload-demo"
          action="/api/document/upload"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          multiple
          :limit="3"
          :on-exceed="handleExceed"
          :file-list="fileList"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="cancelForm">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addDocument } from '@/api/workflow/document'

export default {
  name: 'DocumentCreate',
  data() {
    return {
      temp: {
        title: '',
        type: '',
        content: '',
        attachments: []
      },
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'change' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      },
      typeOptions: [
        { label: '通知公告', value: 1 },
        { label: '规章制度', value: 2 },
        { label: '会议纪要', value: 3 },
        { label: '其他', value: 4 }
      ],
      fileList: []
    }
  },
  methods: {
    submitForm() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addDocument(this.temp).then(() => {
            this.$message({
              type: 'success',
              message: '创建成功'
            })
            this.$router.push('/document/list')
          })
        }
      })
    },
    cancelForm() {
      this.$router.push('/document/list')
    },
    handlePreview(file) {
      console.log(file)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
}
</style> 